import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class SendDeviceData {

    public static void main(String[] args) {
        String THINGSBOARD_URL = "http://localhost:8080/api/v1/{access_token}/telemetry";
        String ACCESS_TOKEN = "n0963XFgnfm9IApixL7V";
        try {
            URL url = new URL(THINGSBOARD_URL.replace("{access_token}", ACCESS_TOKEN));
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("X-Authorization", "Bearer " + ACCESS_TOKEN);
            conn.setDoOutput(true);

            // 要发送的数据
            String data = "{\"longitude\":118.01772,\"latitude\":24.34131 }";
                //String data = "{\"ceshi\": true}";
            // 发送HTTP POST请求
            conn.getOutputStream().write(data.getBytes());

            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new RuntimeException("发送失败 : HTTP error code : " + conn.getResponseCode());
            }

            // 打印响应内容
            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            String output;
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
向thingsboard发送遥测数据
